package com.netease.financial.base.update;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

import com.netease.financial.R;
import com.netease.financial.viewmodel.AppUpdateViewModel;


public class AppUpdateDialog extends DialogFragment {

    private AppUpdateViewModel appUpdateViewModel;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(appUpdateViewModel.getUpdateTips());
        builder.setMessage(appUpdateViewModel.getChangeLog())
                .setPositiveButton(R.string.dialogPositiveButton, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        goToDownload(appUpdateViewModel);
                        dismiss();
                    }
                })
                .setNegativeButton(R.string.dialogNegativeButton, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                    }
                });
        return builder.create();
    }

    public void setUpdateDetail(AppUpdateViewModel viewModel) {
        appUpdateViewModel = viewModel;
    }

    private void goToDownload(AppUpdateViewModel viewModel) {
        Intent intent = new Intent(getActivity().getApplicationContext(), ApkDownloadService.class);
        intent.putExtra(ApkDownloadService.ARG_APK_URL, viewModel.getApkUrl());
        getActivity().startService(intent);
    }
}
